#include<SPI.h>
static uint8_t data[4] = {0x00, 0x00, 0x00, 0x00};
static uint8_t i = 1;
static int CE = 10;

int switchPin1 = 7;
int switchPin2 = 8;
int switchPin3 = 9;

int value = 0;
//关灯
//晚安
//红灯
//读书
//读书+
//绿灯
//蓝灯

const int color_cyan = 0;
const int color_red = 1;
const int color_green = 2;
const int color_blue = 3;
//const int color_blue = 4;

const int modeSleep = 0;      //2x2 红色
const int modeReadBookAdd = 1;//8x8 青色
const int modeReadBook = 2;   //4x4
const int modeReadReduce = 3; //2x2
const int modeRed = 4;
const int modeGreen = 5;
const int modeBlue = 6;
const int modeOff = 7;
//const char mode_white_ = '8';

const uint8_t mode_sleep[8] = {0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00};
const uint8_t mode_readBookReduceAdd[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
const uint8_t mode_readBook[8] = {0x00, 0x00, 0x3c, 0x3c, 0x3c, 0x3c, 0x00, 0x00};
const uint8_t mode_readBookReduce[8] = {0x00, 0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x00};
const uint8_t mode_red[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
const uint8_t mode_green[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
const uint8_t mode_blue[8] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
const uint8_t mode_off[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

void show(int mode_num){
     switch(mode_num){
         case modeSleep:
            setLightColor(color_red, mode_sleep);
            break;
        case modeReadBookAdd:
            setLightColor(color_cyan, mode_readBookReduceAdd);
            break;
        case modeReadBook:
            setLightColor(color_cyan, mode_readBook);
            break;
        case modeReadReduce:
            setLightColor(color_cyan, mode_readBookReduce);
            break;
        case modeRed:
            setLightColor(color_red, mode_red);
            break;     
        case modeGreen:
            setLightColor(color_green, mode_green);
            break;
        case modeBlue:
            setLightColor(color_blue, mode_blue);
            break;   
        case modeOff:
            off();
            break;
    }
}

void setLightColor(int color_num, uint8_t light[]){
    switch(color_num){
        case color_cyan: 
            for(i = 0; i < 8; i++){
                data[0] = 0xff;
                data[1] = ~light[i];
                data[2] = ~light[i];
                data[3] = 0x01 << i;
                digitalWrite(CE, LOW);
                SPI.transfer(data[0]);
                SPI.transfer(data[1]);
                SPI.transfer(data[2]);
                SPI.transfer(data[3]);
                digitalWrite(CE, HIGH);
                delay(2);
            }
            break;
        
        case color_red:
            for(i = 0; i < 8; i++){
                data[0] = ~light[i];
                data[1] = 0xff;
                data[2] = 0xff;
                data[3] = 0x01 << i;

                digitalWrite(CE, LOW);
                SPI.transfer(data[0]);
                SPI.transfer(data[1]);
                SPI.transfer(data[2]);
                SPI.transfer(data[3]);
                digitalWrite(CE, HIGH);

                delay(2);
            }
            break;
            
        case color_green:
            for(i = 0; i < 8; i++){
                data[0] = 0xff;
                data[1] = 0xff;
                data[2] = ~light[i];
                data[3] = 0x01 << i;

                digitalWrite(CE, LOW);
                SPI.transfer(data[0]);
                SPI.transfer(data[1]);
                SPI.transfer(data[2]);
                SPI.transfer(data[3]);
                digitalWrite(CE, HIGH);
                delay(2);
            }
            
            break;
            
        case color_blue:   
            for(i = 0; i < 8; i++){
                data[0] = 0xff;
                data[1] = ~light[i];
                data[2] = 0xff;
                data[3] = 0x01 << i;
                digitalWrite(CE, LOW);
                SPI.transfer(data[0]);
                SPI.transfer(data[1]);
                SPI.transfer(data[2]);
                SPI.transfer(data[3]);
                digitalWrite(CE, HIGH);

                delay(2);
            }
            break;
    }
}


void off(){
    int i;

    uint8_t light_data[4] = {0xff, 0xff, 0xff, 0xff};

    for(i = 0; i < 8; i++){
        data[3] = 0x01 << i;
        digitalWrite(CE, LOW);
        SPI.transfer(light_data[0]);
        SPI.transfer(light_data[1]);
        SPI.transfer(light_data[2]);
        SPI.transfer(light_data[3]);
        digitalWrite(CE, HIGH);
        delay(2);
    }
}

void setup() {
  // put your setup code here, to run once:
  pinMode(CE, OUTPUT);

  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);
  pinMode(switchPin3, INPUT);

  SPI.begin();

  Serial.begin(9600);
}

void loop() {

  int value1 = digitalRead(switchPin1);
  int value2 = digitalRead(switchPin2);
  int value3 = digitalRead(switchPin3);

  value = value1*4 + value2*2 + value3;

  // put your main code here, to run repeatedly:
  Serial.println(value);
  show(value);

  delay(200);
}